package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpAttendanceAddDTO;
import org.jsola.hr.dto.EmpAttendanceUpdateDTO;
import org.jsola.hr.query.EmpAttendanceQuery;
import org.jsola.hr.service.IEmpAttendanceService;
import org.jsola.hr.vo.EmpAttendanceListVO;
import org.jsola.hr.vo.EmpAttendanceVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 员工考勤表
 *
 * @author zhr
 */
@RestController("hrEmpAttendanceController")
@RequestMapping(HrConstants.API_PATH + "/v1/empAttendances")
public class EmpAttendanceController implements IEmpAttendanceControllerAPI {

    @Autowired
    private IEmpAttendanceService empAttendanceService;

    @Override
    @PostMapping
    public Result<EmpAttendanceVO> save(@Valid @RequestBody EmpAttendanceAddDTO empAttendanceAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工考勤表权限校验

        return Result.success(
                empAttendanceService.save(empAttendanceAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpAttendanceUpdateDTO empAttendanceUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工考勤表权限校验

        int count = empAttendanceService.update(empAttendanceUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{empAttendanceId}")
    public Result<Integer> delete(@PathVariable Long empAttendanceId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工考勤表权限校验

        int count = empAttendanceService.deleteByIds(tokenUser, empAttendanceId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] empAttendanceIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工考勤表权限校验

        int count = empAttendanceService.deleteByIds(tokenUser, empAttendanceIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empAttendanceId}")
    public Result<EmpAttendanceVO> findById(@PathVariable Long empAttendanceId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤表权限校验

        return Result.success(
                empAttendanceService.selectById(empAttendanceId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpAttendanceListVO>> listAll(@Valid EmpAttendanceQuery empAttendanceQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤表权限校验

        return Result.success(
                empAttendanceService.select(empAttendanceQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @PostMapping("/page")
    public Result<Page<EmpAttendanceListVO>> page(@Valid @RequestBody EmpAttendanceQuery empAttendanceQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empAttendanceService.selectPage(empAttendanceQuery, tokenUser.getSiteId())
        );
    }


}

